#include "VideoRenderer.h"

namespace TUMAR { namespace Render {
	VideoRenderer::VideoRenderer() : height(480), width(640)
	{
	}

	VideoRenderer::~VideoRenderer(void)
	{
	}

	void VideoRenderer::OnImageCaptured(const cv::Mat& image)
	{
		cv::Mat tmp;

		cv::flip(image, tmp, 0);

		if(image.size().height != this->height || image.size().width != this->width)
		{
			cv::resize(tmp, this->buffer, cv::Size(this->width, this->height));
		}
		else
		{
			this->buffer = tmp;
		}
	}

	void VideoRenderer::Resize(int width, int height)
	{
		this->width = width;
		this->height = height;
	}

	void VideoRenderer::Render(void)
	{
		cv::Size imageSize = this->buffer.size();

		glMatrixMode(GL_PROJECTION);
		glPushMatrix();

		glLoadIdentity();
		gluOrtho2D(0, imageSize.width, 0, imageSize.height);
		
		{
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();

			glRasterPos2i(0, 0);
			glDrawPixels(imageSize.width, imageSize.height, GL_BGR_EXT, GL_UNSIGNED_BYTE, this->buffer.data);

			glPopMatrix();
		}
		
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
	}
}}